﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
    public partial class FormCalculator : Form
    {
        public FormCalculator()
        {
            InitializeComponent();
        }

        private string operation;
        private decimal leftOperand;
        private decimal rightOperand;
        private bool isFirst;

        public bool IsFirst
        {
            get
            {
                return isFirst;
            }
            set
            {
                isFirst = value;
            }
        }

        public string Operation
        {
            get
            {
                return operation;
            }
            set
            {
                operation = value;
            }
        }

        public decimal LeftOperand
        {
            get
            {
                return leftOperand;
            }
            set
            {
                leftOperand = value;
            }
        }

        public decimal RightOperand
        {
            get
            {
                return rightOperand;
            }
            set
            {
                rightOperand = value;
            }
        }

        private void buttonClean_Click(object sender, EventArgs e)
        {
            textBoxResult.Text = "0";
            this.leftOperand = 0;
            this.IsFirst = false;
        }

        private void buttonNumber0_Click(object sender, EventArgs e)
        {
            if (textBoxResult.Text != "0")
            {
                textBoxResult.Text += ((Button)sender).Text;
            }
        }

        private void buttonNumber_Click(object sender, EventArgs e)
        {
            if (textBoxResult.Text == "0")
            {
                textBoxResult.Text = ((Button)sender).Text;
            }
            else
            {
                if (!isFirst)
                {
                    textBoxResult.Text = ((Button)sender).Text;
                    isFirst = true;
                }
                else
                {
                    textBoxResult.Text += ((Button)sender).Text;
                }
            }
        }

        private void buttonDivide_Click(object sender, EventArgs e)
        {
            if (this.operation != String.Empty)
            {
                buttonEquals_Click(null, null);
            }
            this.operation = "/";
            decimal.TryParse(textBoxResult.Text, out this.leftOperand);
            this.IsFirst = false;
        }

        private void buttonMul_Click(object sender, EventArgs e)
        {
            if (this.operation != String.Empty)
            {
                buttonEquals_Click(null, null);
            }
            this.operation = "*";
            decimal.TryParse(textBoxResult.Text, out this.leftOperand);
            this.IsFirst = false;
        }

        private void buttonExtract_Click(object sender, EventArgs e)
        {
            if (this.operation != String.Empty)
            {
                buttonEquals_Click(null, null);
            }
            this.operation = "-";
            decimal.TryParse(textBoxResult.Text, out this.leftOperand);
            this.IsFirst = false;
        }

        private void buttonSum_Click(object sender, EventArgs e)
        {
            if (this.operation != String.Empty)
            {
                buttonEquals_Click(null, null);
            }
            this.operation = "+";
            decimal.TryParse(textBoxResult.Text, out this.leftOperand);
            this.IsFirst = false;
        }

        private void buttonEquals_Click(object sender, EventArgs e)
        {
            decimal.TryParse(textBoxResult.Text, out this.rightOperand);
            switch (this.operation)
            {
                case "+": textBoxResult.Text = (this.leftOperand + this.rightOperand).ToString();
                    this.Operation = String.Empty; break;
                case "-": textBoxResult.Text = (this.leftOperand - this.rightOperand).ToString();
                    this.Operation = String.Empty; break;
                case "*": textBoxResult.Text = (this.leftOperand * this.rightOperand).ToString();
                    this.Operation = String.Empty; break;
                case "/": textBoxResult.Text = (this.leftOperand / this.rightOperand).ToString();
                    this.Operation = String.Empty; break;
            }
        }
    }
}
